from django.core.management.base import BaseCommand
from django.core.management import call_command
from stocksys import models as stocksys_models


class Command(BaseCommand):
    help = 'init_exchange_data'

    def add_arguments(self, parser):
        parser.add_argument('--create',
                            type=str,
                            choices=['all', 'fund', 'stock', 'market_index', 'industry'],
                            default='all',
                            help='Create exchange data \n eg:\n --create_data all')
        parser.add_argument('--delete',
                            action='store_true',
                            dest='delete',
                            default=False,
                            help='Delete exchange data')
        parser.add_argument('--method',
                            type=str,
                            choices=['jqdata', 'sina'],
                            default='jqdata',
                            help='Data request method')

        # parser.add_argument('--clear_exchange_data', nargs='+', type=int)

    def handle(self, *args, **options):
        self.stdout.write(self.style.SUCCESS(options))
        if options.get('--delete'):
            stocksys_models.MarketIndex.objects.delete()
            stocksys_models.Industry.objects.delete()
            stocksys_models.Concept.objects.delete()
            stocksys_models.Stock.objects.delete()
            stocksys_models.Fund.objects.delete()
        if options.get('--method') == 'jqdata':
            call_command('create_industry_data_from_jqdata')  # 行业数据
            call_command('create_stock_data_from_jqdata')  # 股票数据
            call_command('create_conceptual_data_from_jqdata')  # 概念数据

            call_command('create_market_index_data_from_jqdata')  # 大盘数据
            call_command('create_fund_data_from_jqdata')  # 基金数据
        elif options.get('--method') == 'sina':
            pass
